<template>
  <div class="login-container">
    <!-- 头部 -->
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="../../assets/images/logo.png" />
      </template>
    </van-nav-bar>
    <!-- 登录表单 -->
    <div>
      <van-form @submit="onSubmitFn">
        <van-field
          v-model="formLogin.mobile"
          type="tel"
          label="手机号"
          placeholder="请输入手机号"
          required
          :rules="rules.tel"
        />
        <van-field
          v-model="formLogin.code"
          type="password"
          label="密码"
          placeholder="请输入密码"
          required
          :rules="rules.password"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            :loading="isLoading"
            :disabled="isLoading"
            loading-text="加载ing..."
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
// import { loginAPI } from '../../api/login'
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      isLoading: false,
      formLogin: {
        mobile: '13888888888',
        code: '246810' // 默认只能用246810后台规定了
      },
      rules: {
        tel: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'onChange'
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: 'onChange'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'onChange'
          },
          {
            pattern: /^\d{6}$/,
            message: '请输入正确的验证码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  // },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmitFn () {
      this.isLoading = true
      try {
        await this.asyncLoginAction({
          tel: this.formLogin.mobile,
          pwd: this.formLogin.code
        })
        // console.log(resp)
        Notify({ type: 'success', message: '登录成功' })
        this.$router.replace({
          path: this.$route.query.from || '/layout'
        })
      } catch (err) {
        Notify({ type: 'danger', message: '验证失败' })
      }
      this.isLoading = false
      //   console.log(resp)

      // 登录成功后跳转到首页
      this.$router.replace({
        path: '/layout'
      })
    }
  }
}
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
// .van-nav-bar {
//   background: #007bff;
//   /deep/ .van-nav-bar__title {
//   color: #fff;
// }
// }
</style>
